﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using BLL;
using Client.Model;
using Client.QuizService;
using Client.View;

namespace Client.ViewModel
{
    public class QuizViewModel : BaseViewModel
    {
        #region Bound properties
        
        private Quiz _quiz;
        public Quiz Quiz
        {
            get
            {
                return _quiz;
            }
            set
            {
                _quiz = value;
                OnPropertyChanged("Quiz");
            }
        }

        private ObservableCollection<QuestionModel> _questions;
        public ObservableCollection<QuestionModel> Questions
        {
            get
            {
                return _questions ?? (_questions = new ObservableCollection<QuestionModel>());
            }
        }

        private bool _isNextVisible;
        public bool IsNextVisible
        {
            get
            {
                return _isNextVisible;
            }
            set
            {
                _isNextVisible = value;
                OnPropertyChanged("IsNextVisible");
            }
        }

        private bool _isPrevVisible;
        public bool IsPrevVisible
        {
            get
            {
                return _isPrevVisible;
            }
            set
            {
                _isPrevVisible = value;
                OnPropertyChanged("IsPrevVisible");
            }
        }

        #endregion

        #region Other properties

        public ScrollViewer ScrollViewer { get; set; }

        public int Score
        {
            get
            {
                if (Questions != null && Questions.Count > 0)
                {
                    var score = 0;
                    foreach (var question in Questions)
                    {
                        var point = 0;
                        if (question.GivenAnswer == question.Question.RightAnswer)
                            point = question.Question.Point;
                        score += point;
                    }
                    return score;
                }
                return 0;
            }
            
        }

        public int TotalScore
        {
            get
            {
                if (Questions != null && Questions.Count > 0)
                {
                    return Questions.Sum(questionModel => questionModel.Question.Point);
                }
                return 0;
            }

        }

        private int _questionIndex = -1;

        #endregion

        #region Commands
        private DelegateCommand<object> _nextCommand;
        public DelegateCommand<object> NextCommand
        {
            get { return _nextCommand ?? (_nextCommand = new DelegateCommand<object>(NextQuestion)); }
        }
        
        private DelegateCommand<object> _prevCommand;
        public DelegateCommand<object> PrevCommand
        {
            get { return _prevCommand ?? (_prevCommand = new DelegateCommand<object>(PrevQuestion)); }
        }

        #endregion

        public QuizViewModel(int quizId)
        {
            try
            {
                using (var srv = new QuizServiceClient())
                {
                    var quiz = srv.GetQuiz(quizId);
                    if (quiz != null)
                    {
                        this.Quiz = quiz;
                        if (Quiz != null && Quiz.Questions != null && Quiz.Questions.Count > 0)
                        {
                            _questionIndex = 0;
                            foreach (var question in Quiz.Questions)
                            {
                                Questions.Add(new QuestionModel
                                                  {
                                                      Question = question,
                                                      GivenAnswer = -1
                                                  });
                            }

                            IsNextVisible = quiz.Questions != null && quiz.Questions.Count > 1;
                            IsPrevVisible = false;
                            OnPropertyChanged("Questions");
                        }
                    }
                }
            }
            catch
            {
                MessageBox.Show("Hát ez nem sikerült, bocsi.");
            }
        }

        private void NextQuestion(object param)
        {
            IsNextVisible = Quiz.Questions != null && Quiz.Questions.Count > (++_questionIndex + 1) ;
            IsPrevVisible = true;

            if (ScrollViewer != null)
            {
                ScrollViewer.ScrollToHorizontalOffset(ScrollViewer.HorizontalOffset + 1 );
            }
        }

        private void PrevQuestion(object param)
        {
            IsNextVisible = true;
            IsPrevVisible = Quiz.Questions != null && (--_questionIndex) > 0;

            if (ScrollViewer != null)
                ScrollViewer.ScrollToHorizontalOffset(ScrollViewer.HorizontalOffset - 1);
        }
    }
}
